package com.hthl.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
@EnableCaching
@PropertySource(value = "classpath:application.properties")
public class ArticleRedisConfig extends RedisConfig {

    @Value("${spring.redis3.database}")
    private int dbIndex;
    @Value("${spring.redis3.host}")
    private String host;
    @Value("${spring.redis3.port}")
    private int port;
    @Value("${spring.redis.timeout}")
    private int timeout;
    @Value("${spring.redis3.password}")
    private String password;

    @Bean
    @Qualifier
    public JedisConnectionFactory articleRedisConnectionFactory() {
        return newJedisConnectionFactory(dbIndex, host, port, timeout, password);
    }

    @Qualifier
    @Bean(name = "articleRedisTemplate")
    public RedisTemplate articleRedisTemplate() {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(articleRedisConnectionFactory());
        setSerializer(template); //设置序列化工具，这样ReportBean不需要实现Serializable接口
        template.afterPropertiesSet();
        return template;
    }

    @Qualifier
    @Bean(name = "articleStringRedisTemplate")
    public StringRedisTemplate articleStringRedisTemplate() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(articleRedisConnectionFactory());
        template.afterPropertiesSet();
        return template;
    }

}
